package com.autumn.jvm.jvm;

/**
 * 栈上分配，标量替换
 * 代码调用了1亿次alloc()，若是分配到堆上，大致需1G以上堆内存，若堆内存小于该值，必然触发GC
 * 
 * 使用如下参数则不会触发GC
 * -Xmx15m -Xms15m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:+EliminateAllocations
 * 使用如下参数则引发大量GC
 * -Xmx15m -Xms15m -XX:-DoEscapeAnalysis -XX:+PrintGC -XX:+EliminateAllocations
 * -Xmx15m -Xms15m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:-EliminateAllocations
 */
public class AllotoOnStack {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            alloc();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
    }

    private static void alloc() {
        User user = new User(1, "张三");
    }
}
